#pragma once
#include <QObject>
#include <QString>
#include <QMutex>
#include <QFile>
#include <QTimer>
#include <QDate>
#include <QVector>
#include <QSharedPointer>

enum {
	LOG_DEBUG,
	LOG_INFO,
	LOG_WARNING,
	LOG_ERROR,
	LOG_FATAL,
	LOG_LEVEL_NUM
};

enum {
	LOG_MODULE_GENERAL,	
	LOG_MODULE_NUM,
};

class HhLogObject 
{
public:
	QString time;
	int module;
	int level;
	QString message;
};
typedef QSharedPointer<HhLogObject> HhLogObjectPtr;

class LogManager : public QObject
{
	Q_OBJECT

public:		
	static LogManager *instance();		

	void init(QString logDir);
	void deinit();
	void setLogDelInfo(bool isDel, int delTime);
	
	QString name(int module_idx);
	void log(int module, int level, const char *format, ...);

signals:
	void newLog(HhLogObjectPtr log);

protected slots:
	void onTimeOut();

protected:
	struct ModuleInfo {		
		QFile *file = NULL;
		QString name;
	};
	struct LogInfo
	{
		QDateTime time;
		QString path;
	};

	LogManager();
	~LogManager();
	void createFile();
	void closeFile();
		
	QVector<ModuleInfo> moduleList;		
	QVector<LogInfo> m_logList;
	QString m_logDir;

	bool m_isDel;
	int m_delTime;

	char* logBuffer;
	char* fileBuffer;
	QMutex mutex;
	QTimer timer;
	QDateTime curDate;
	bool m_init;
};

#define LogDebug(...)   LogManager::instance()->log(LOG_MODULE_GENERAL,LOG_DEBUG,__VA_ARGS__)
#define LogInfo(...)    LogManager::instance()->log(LOG_MODULE_GENERAL,LOG_INFO,__VA_ARGS__)
#define LogWarning(...) LogManager::instance()->log(LOG_MODULE_GENERAL,LOG_WARNING,__VA_ARGS__)
#define LogError(...)   LogManager::instance()->log(LOG_MODULE_GENERAL,LOG_ERROR,__VA_ARGS__)
#define LogFatal(...)   LogManager::instance()->log(LOG_MODULE_GENERAL,LOG_FATAL,__VA_ARGS__)